<?php
namespace app\modules\admini\components;

use Yii;
use app\modules\admini\components\BUtils;
/**
 * Default controller for the `admini` module
 */
class BModel extends \yii\db\ActiveRecord{
    const DEL_STATE = 1;
    const STATUS = 1;
	/**
     * 验证有错误时输出给前端回调函数
     * @return [type] [description]
     */
    public function afterValidate()
    {
        if($this->hasErrors()){
            $message = '<strong>以下表单填写有误:</strong><br>';
            foreach ($this->getErrors() as $value) {
                $message .= implode(",", $value).'<br>';
            }
            BUtils::callback(array('message'=> $message ), 'error');

        }
    }

    /**
     * 保存数据前统一处理
     * @return boolean true为可保存， false不可保存
     */
   
    public function beforeSave($insert)
    {
        if (parent::beforeSave($insert)) {
            if ($this->isNewRecord) {
                $this->hasAttribute('create_time') && $this->create_time = date("Y-m-d H:i:s");
            }
            $this->hasAttribute('update_time') && $this->update_time = date("Y-m-d H:i:s");
            $this->hasAttribute('operator_id') && $this->operator_id = Yii::$app->user->identity->id;
            return true;
        } else {
            return false;
        }
    }
}
?>